<?php ob_start();
include("config/config.php"); 
///////Google Login////////////////////*
require_once 'google/src/apiClient.php';
require_once 'google/src/contrib/apiOauth2Service.php';

include_once("google/src/config.php");

$client = new apiClient();
$client->setApplicationName("Google UserInfo PHP Starter Application");
// Visit https://code.google.com/apis/console?api=plus to generate your
// oauth2_client_id, oauth2_client_secret, and to register your oauth2_redirect_uri.
 $client->setClientId('391587492941.apps.googleusercontent.com');
 $client->setClientSecret('kn9GbUw9ZG5ahfj089qNii1J');
 $client->setRedirectUri('http://mycitydial.com/myaccount.php');
 $client->setDeveloperKey('insert_your_developer_key');
$oauth2 = new apiOauth2Service($client);

if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['token'])) {
 $client->setAccessToken($_SESSION['token']);
}

if (isset($_REQUEST['logout'])) {
  unset($_SESSION['token']);
  $client->revokeToken();
}

if ($client->getAccessToken()) {
  $user1 = $oauth2->userinfo->get();

//   These fields are currently filtered through the PHP sanitize filters.
//   See http://www.php.net/manual/en/filter.filters.sanitize.php
  $id = filter_var($user['id'], FILTER_SANITIZE_EMAIL);
  $email = filter_var($user['email'], FILTER_SANITIZE_EMAIL);
  $img = filter_var($user['picture'], FILTER_VALIDATE_URL);
  $given_name = filter_var($user['given_name'], FILTER_SANITIZE_EMAIL);
  $family_name = filter_var($user['family_name'], FILTER_SANITIZE_EMAIL);
  $link = filter_var($user['link'], FILTER_SANITIZE_EMAIL);
  $picture = filter_var($user['picture'], FILTER_VALIDATE_URL);
  $gender = filter_var($user['gender'], FILTER_SANITIZE_EMAIL);
  $personMarkup = "$email<div><img src='$img?sz=50'></div>";

//   The access token may have been updated lazily.
  $_SESSION['token'] = $client->getAccessToken();  
  
} else {
  $authUrl = $client->createAuthUrl();
} //print_r($user);
///////Insert Data to db if suscessfull/////
if(isset($user1)&&(!isset($_SESSION['id']))){
	$chk=mysql_query("SELECT * FROM `my_user` WHERE `google_id`='".$user1['id']."' ")or die(mysql_error());	
	if(!mysql_num_rows($chk)){
		$sql=mysql_query("INSERT INTO `my_user` SET `google_id`='".$user1['id']."', 
													`fname`='".$user1['given_name']."',
													`lname`='".$user1['family_name']."',
													`email`='".$user1['email']."',													
													`dob`='".$user1['birthday']."',
													`gender`='".$user1['gender']."' ")or die(mysql_error());
		$id=mysql_insert_id();
		if($sql){ 
			$sql2=mysql_query("SELECT * FROM `my_user` WHERE `id`=$id ")or die(mysql_error());	
			$rows=mysql_fetch_array($sql2);
			$_SESSION['id']=	$rows['id'];
			$_SESSION['fname']=	$rows['fname'];
			header("location:myaccount.php"); exit;
		}
	}else{
		$sql2=mysql_query("SELECT * FROM `my_user` WHERE `google_id`='".$user1['id']."' ")or die(mysql_error());	
		$rows=mysql_fetch_array($sql2);
		$_SESSION['id']=	$rows['id'];
		$_SESSION['fname']=	$rows['fname'];
		header("location:myaccount.php"); exit;
	}	
}
///////End of Google Login/////////////////////////////////////////////
?>